package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.UserComment;
import cn.wolfcode.trip.base.mapper.StrategyCommentMapper;
import cn.wolfcode.trip.base.mapper.UserCommentMapper;
import cn.wolfcode.trip.base.query.StrategyCommentQueryObject;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/11/13.
 */
@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {

    @Autowired
    private StrategyCommentMapper commentMapper;
    @Autowired
    private UserCommentMapper userCommentMapper;

    @Override
    public void save(StrategyComment strategyComment) {
        strategyComment.setCommentTime(new Date());
        commentMapper.insert(strategyComment);
    }

    @Override
    public void setTags(Long commentId, Long[] tagIds) {
        for (Long tagId : tagIds) {
            commentMapper.setTag(commentId,tagId);
        }
    }

    //根据攻略id查询推荐的评论
    @Override
    public PageInfo query(StrategyCommentQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),"sc.likes desc");
        List<StrategyComment> strategyComments = commentMapper.queryForList(qo);
        return new PageInfo(strategyComments);
    }

    //根据id查询攻略评论
    @Override
    public StrategyComment getById(Long id) {
        //查询该评论的评论
        List<UserComment> userCommentList = userCommentMapper.selectByCommentId(id);
        StrategyComment strategyComment = commentMapper.selectByPrimaryKey(id);
        strategyComment.setCommentList(userCommentList);
        return strategyComment;
    }

    @Override
    public List<StrategyComment> listByUserId(Long id) {
        return commentMapper.selectByUserId(id);
    }
}
